﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Model;

namespace AcceptServer.Service
{
    class StationProtocol
    {
        OperateDataBase _operateDataBase = new OperateDataBase();

        public byte[] GetClientVersionInfoData(byte[] realData)
        {
            if (realData.Length==27)
            {                                
                string phone = 
                OperateAgreement.GetPhoneNumInMessageHead(realData);
                double currentversion = BitConverter.ToDouble(realData, 
                    ConstParameters.MBodySatrtIndex);
                if (AcceptMainForm.StationList.Contains(phone))
                {
                    AdStationInfo station = AcceptMainForm.StationList[phone] as AdStationInfo;
                    station.Version = currentversion;    
                }
                else
                {
                    AdStationInfo station = new AdStationInfo();
                    station.Machine.Number = phone;
                    station.UpdateTime = DateTime.Now;
                    station.Version = currentversion;
                    station.IsOnline = true;
                    AcceptMainForm.StationList[phone] = station;
                }
            }
            double version = 1.0;
            string config = OperateConfig.GetTheNewestClientVersion();
            double.TryParse(config, out version);
            byte[] messageBody = BitConverter.GetBytes(version);

            byte[] messageHead = OperateAgreement.GetMessageHead(ConstParameters.CmdStationClientVersionReply,
                false, false, messageBody.Length, new byte[6], new byte[2],
                                                 1, 1);
            List<byte> messageDatas = new List<byte>();
            messageDatas.AddRange(OperateAgreement.PackageSendData(messageHead, messageBody.ToArray()));
            return messageDatas.ToArray();
        }
        
        public byte[] GetXmlVersion(byte[] realData)
        {
            int version = 0;
            string config = OperateConfig.GetStationXmlVersion();
            Int32.TryParse(config, out version);
            byte[] messageBody = BitConverter.GetBytes(version);
            byte[] messageHead = OperateAgreement.GetMessageHead(
                ConstParameters.CmdStationGetNewestXmlVersionReply,
                false, false, messageBody.Length, new byte[6], new byte[2],
                                                 1, 1);
            List<byte> messageDatas = new List<byte>();
            messageDatas.AddRange(OperateAgreement.PackageSendData(messageHead, messageBody.ToArray()));
            return messageDatas.ToArray();
        }

        public byte[] GetStationsListData(byte[] realData)
        { 
             
            List<byte> messageBody = new List<byte>();          
            lock (AcceptMainForm.StationList.SyncRoot)
            {
                foreach (AdStationInfo station in AcceptMainForm.StationList.Values)
                {                   
                    byte[] number = CommonRoutine.BcdStringToBytes(station.Machine.Number);
                    messageBody.AddRange(number);
                    float version = (float)station.Version;
                    byte[] bytesversion = BitConverter.GetBytes(version);
                    messageBody.AddRange(bytesversion);
                }
            }             
            byte[] messageHead = OperateAgreement.GetMessageHead(ConstParameters.CmdGetStationListReply,
                false, false, messageBody.Count, new byte[6], new byte[2],
                                                 1, 1);
            List<byte> messageDatas = new List<byte>();
            messageDatas.AddRange(OperateAgreement.PackageSendData(messageHead, messageBody.ToArray()));
            return messageDatas.ToArray();
        }


        public byte[] GetAdUrlData(byte[] realData)
        {
            string phone =
            OperateAgreement.GetPhoneNumInMessageHead(realData);
            string url = _operateDataBase.GetAdMachineAdUrlByNumber(phone.Remove(0,1));
            byte[] messageBody = Encoding.GetEncoding("GBK").GetBytes(url);
             
            byte[] messageHead = OperateAgreement.GetMessageHead(ConstParameters.CmdGetStationAdUrlListReply,
              false, false, messageBody.Length, new byte[6], new byte[2],
                                                 1, 1);
            List<byte> messageDatas = new List<byte>();
            messageDatas.AddRange(OperateAgreement.PackageSendData(
                messageHead, messageBody));
            return messageDatas.ToArray();
        }


    }
}
